package com.bookstore.controller;

import com.bookstore.domain.Address;
import com.bookstore.domain.Error;
import com.bookstore.service.AddressService;
import com.bookstore.utils.JsonUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/address")
public class AddressController {
    @Autowired
    private AddressService addressService;

    //用于填充页面的 地址信息下拉列表
    @RequestMapping("/getAddress.do")
    @ResponseBody
    public String getAddress(Integer uid) {
        List<Address> adds = addressService.findAllByUid(uid);
        return JsonUtils.writeValueAsString(adds);
    }

    //地址管理
    @RequestMapping("/getAddressByUid.do")
    @ResponseBody
    public String getAddressByUid(Integer start, Integer uid) {
        //处理数据
        if (start == null) {
            start = 1;
        }
        //从第一页开始，一页5行
        PageHelper.startPage(start, 5);
        List<Address> adds = addressService.findAllByUid(uid);
        PageInfo<Address> pages = new PageInfo<Address>(adds);
        return JsonUtils.writeValueAsString(pages);
    }

    //添加新地址
    @RequestMapping("/addAddress.do")
    @ResponseBody
    public String addAddress(Address address) {
        System.err.println(address);
        Error er = new Error();
        int result = 0;
        result = addressService.addAddress(address);
        if (result == 0) {
            er.setErrorType(6);
            er.setErrorInfo("网络开小差了。。");
        } else if (result == 1) {
            er.setErrorType(0);
            er.setErrorInfo("添加成功！");
        }
        return JsonUtils.writeValueAsString(er);
    }

    //根据地址id获取地址信息
    @RequestMapping("/getAddressOne.do")
    @ResponseBody
    public String getAddressById(Integer addressId){
        Error er = new Error();
        Address address = addressService.getOneAddress(addressId);
        return JsonUtils.writeValueAsString(address);
    }

    @RequestMapping("/upAddress.do")
    @ResponseBody
    public String upAddress(Address address) {
        System.err.println(address);
        Error er = new Error();
        int result = 0;
        result = addressService.upAddress(address);
        if (result == 0) {
            er.setErrorType(6);
            er.setErrorInfo("网络开小差了。。");
        } else if (result == 1) {
            er.setErrorType(1);
            er.setErrorInfo("修改成功！");
        }
        return JsonUtils.writeValueAsString(er);
    }


    @RequestMapping("/delAddress")
    @ResponseBody
    public String upAddress(Integer addressId) {
//        System.err.println(addressId);
        Error er = new Error();
        int result = 0;
        result = addressService.delAddress(addressId);
        if (result == 0) {
            er.setErrorType(6);
            er.setErrorInfo("网络开小差了。。");
        } else if (result == 1) {
            er.setErrorType(0);
            er.setErrorInfo("删除成功！");
        }
        return JsonUtils.writeValueAsString(er);
    }
}
